package cn.interestingshop.servlet;

import cn.interestingshop.entity.Goods;
import cn.interestingshop.service.GoodsService;
import cn.interestingshop.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/GoodsSelectAllServlet")
public class GoodsSelectAllServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取请求参数
        String pageNo = request.getParameter("pageNo");
        if(pageNo==null || "".equals(pageNo)){
            pageNo="1";
        }
        int pageSize=3;
        //3.调用service进行业务处理
       GoodsService service=
                new GoodsServiceImpl();
        //查询每页显示的数据
        List<Goods> list = service.selectAll(Integer.parseInt(pageNo), pageSize);
        //总记录数
        int totalCount = service.selectCount();
        //计算总页数
        int totalPage=0;
        if(totalCount%pageSize==0){
            totalPage=totalCount/pageSize;
        }else{
            totalPage=totalCount/pageSize+1;
        }
        //4.分析返回结果
        request.setAttribute("pageNo",pageNo);
        request.setAttribute("list",list);
        request.setAttribute("totalCount",totalCount);
        request.setAttribute("totalPage",totalPage);

        request.getRequestDispatcher("/back/Member_GoodList.jsp")
                .forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
